<script>
	import {
		getCustomerWeChatPermission,
		getEnableYuewanSwitch,
		getLogin
	} from '@/constant/login.js';
	import {
		wechatAppVersion
	} from '@/config/config.js';

	export default {
		onLaunch: function() {

			this.getCustomerWeChatPermission();
			this.getToken()
			// this.getEnableYuewanSwitch();
			this.onLoadFonts();
			if (wx.canIUse('getUpdateManager')) {
				const updateManager = wx.getUpdateManager();
				updateManager.onCheckForUpdate(function(res) {
					// 请求完新版本信息的回调
					if (res.hasUpdate) {
						updateManager.onUpdateReady(function() {
							wx.showModal({
								title: '更新提示',
								content: '新版本已经准备好，是否重启应用？',
								success: function(res) {
									if (res.confirm) {
										updateManager.applyUpdate();
									}
								}
							});
						});
						updateManager.onUpdateFailed(function() {
							// 新的版本下载失败
							wx.showModal({
								title: '已经有新版本了哟~',
								content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
							});
						});
					}
				});
			}
		},
		onShow: function() {},
		onHide: function() {},
		methods: {
			getToken() {
				var that = this;
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						console.log(loginRes);
						if (loginRes.code) {
							let res_code = loginRes.code;
							uni.getUserInfo({
								provider: 'weixin',
								success: function(infoRes) {
									console.log(infoRes, "infoRes--------------");
									let loginInfo = {
										code: res_code,
										rawData: infoRes.rawData,
										signature: infoRes.signature,
										city: infoRes.userInfo.city,
										province: infoRes.userInfo.province,
										encryptedData: infoRes.encryptedData,
										iv: infoRes.iv,
										nickName: infoRes.userInfo.nickName,
										gender: infoRes.userInfo.gender,
										avatarUrl: infoRes.userInfo.avatarUrl,
										type: 34,
										state: 'default'
									};
									that.wechatLogin(loginInfo);
								}
							});
						}
					},
					fail: function(loginRes) {
						console.log('获取微信用户信息失败', loginRes);
					}
				});
			},
			async wechatLogin(obj) {
				let res = await getLogin(obj);
				//uni.setStorageSync('customerId', res.customer.id);
				uni.setStorageSync('userToken', res.accessToken);
				//	uni.setStorageSync('customerInfo', res.customer);
				// await this.getCustomerWeChatPermission();
				// if (!res.Phone) {
				// 	this.openWechatMobilePopup();
				// 	return;
				// }
				// uni.reLaunch({
				// 	url: '../mine/index'
				// });
			},
			async getCustomerWeChatPermission() {
				try {
					var token = uni.getStorageSync('userToken');
					if (!token) {
						return;
					}
					let res = await getCustomerWeChatPermission();
					uni.setStorageSync('userPermission', res);
				} catch (e) {}
			},
			async getEnableYuewanSwitch() {
				try {
					let res = await getEnableYuewanSwitch({
						wechatAppVersion: wechatAppVersion
					});
					uni.setStorageSync('wetchatEnableYueWan', res);
				} catch (e) {}
			},
			onLoadFonts() {
				setTimeout(() => {
					const url1 = 'https://at.alicdn.com/t/webfont_z206fw1z3h.ttf';
					const url2 = 'https://at.alicdn.com/t/webfont_bdv0gqlajeu.ttf';
					uni.loadFontFace({
						global: true,
						family: 'SourceHanSansCN-Medium',
						source: `url('${url1}')`
					});
					uni.loadFontFace({
						global: true,
						family: 'SourceHanSansCN-Regular',
						source: `url('${url2}')`
					});
				}, 300);
			}
		}
	};
</script>

<style>
	@import url(./css/common.css);

	/* 解决头条小程序组件内引入字体不生效的问题 */
	/* #ifdef MP-TOUTIAO */
	@font-face {
		font-family: uniicons;
		src: url('/static/uni.ttf');
	}

	/* #endif */

	.bottom-change-view {
		background: linear-gradient(to right, #FFD90B, #FF7C0B) !important;
	}

	button::after {
		border-radius: unset;
		border: unset;
	}

	.select-protocol checkbox .wx-checkbox-input {
		border-radius: 10rpx !important;
		color: #fd9f07 !important;
		border: 2rpx solid #fd9f07 !important;
	}

	.radio-item-label radio .wx-radio-input {
		color: #fd9f07 !important;
		border: 2rpx solid #fd9f07 !important;
		background: transparent;
		transform: scale(0.9);
	}

	.radio-item-label radio .wx-radio-input.wx-radio-input-checked::before {
		border-radius: 50%;
		/* 圆角 */
		text-align: center;
		color: #fd9f07 !important;
		/* 对勾颜色 白色 */
		transform: translate(-50%, -45%) scale(0.8);
	}
</style>